import type { RequiredDeep } from 'type-fest'

import { ajax } from '@/utils'

import type { components } from '../schema'
import type { IDeleteReq, IExportRes } from '../type'

/**
 * 角色接口
 */
export const roleApi = {
  /**
   * 获取角色分页
   */
  getRolePage(data: IGetRolePageReq): Promise<IGetRolePageRes> {
    return ajax.post('/system/role/page', data)
  },
  /**
   * 添加角色
   */
  createRole(data: ICreateRoleReq) {
    return ajax.post('/system/role/create', data)
  },
  /**
   * 更新角色
   */
  updateRole(id: string, data: IUpdateRoleReq) {
    return ajax.put('/system/role/update', data, { params: { id } })
  },
  /**
   * 删除角色
   */
  deleteRole(data: IDeleteReq) {
    return ajax.delete('/system/role/delete', { data })
  },
  /**
   * 导出角色分页
   */
  exportRolePage(data: IGetRolePageReq): Promise<IExportRes> {
    return ajax.post('/system/role/export', data)
  },
  /**
   * 切换角色状态
   */
  toggleRoleStatus(id: string) {
    return ajax.put('/system/role/toggleStatus', null, { params: { id } })
  },
  /**
   * 获取已分配角色分页
   */
  getAllocatedPage(
    id: string,
    data: IGetAllocatedPageReq
  ): Promise<IGetAllocatedPageRes> {
    return ajax.post('/system/role/allocatedPage', data, { params: { id } })
  },
  /**
   * 撤销分配角色
   */
  cancelAllocated(id: string, data: IDeleteReq) {
    return ajax.put('/system/role/cancelAllocated', data, {
      params: { id }
    })
  },
  /**
   * 获取未分配角色分页
   */
  getUnallocatedPage(
    id: string,
    data: IGetUnallocatedPageReq
  ): Promise<IGetUnallocatedPageRes> {
    return ajax.post('/system/role/unallocatedPage', data, {
      params: { id }
    })
  },
  /**
   * 分配角色
   */
  allocate(id: string, data: IAllocateReq) {
    return ajax.put('/system/role/allocate', data, { params: { id } })
  }
}

export type IGetRolePageReq = components['schemas']['FindRolePageDTO']
export type IGetRolePageRes = RequiredDeep<
  components['schemas']['FindRolePageVO']['data']
>
export type IRolePageItem = IGetRolePageRes['list'][0]
export type ICreateRoleReq = components['schemas']['CreateRoleDTO']
export type IUpdateRoleReq = components['schemas']['UpdateRoleDTO']
export type IGetAllocatedPageReq = components['schemas']['FindAllocatedPageDTO']
export type IGetAllocatedPageRes = RequiredDeep<
  components['schemas']['FindAllocatedPageVO']['data']
>
export type IAllocatedPageItem = IGetAllocatedPageRes['list']['0']
export type IGetUnallocatedPageReq =
  components['schemas']['FindUnallocatedPageDTO']
export type IGetUnallocatedPageRes = RequiredDeep<
  components['schemas']['FindUnallocatedPageVO']['data']
>
export type IAllocateReq = components['schemas']['AllocateDTO']
